{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "import numpy as np\n",
    "import skimage.measure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate(dim):\n",
    "    data = np.zeros((dim, dim, dim), dtype=np.uint8)\n",
    "\n",
    "    x = np.linspace(-0.5, 0.5, dim, dtype=np.float32)\n",
    "    y = np.linspace(-0.5, 0.5, dim, dtype=np.float32)\n",
    "    z = np.linspace(-0.5, 0.5, dim, dtype=np.float32)\n",
    "\n",
    "    x, y, z = np.meshgrid(x, y, z)\n",
    "\n",
    "    c, s = np.cos(1.5 * x), np.sin(1.5 * x)\n",
    "\n",
    "    my = y * c - z * s \n",
    "    mz = y * s + z * c\n",
    "\n",
    "    my = np.fmod(my + 0.5, 0.333) * 3 - 0.5\n",
    "    mz = np.fmod(mz + 0.5, 0.333) * 3 - 0.5\n",
    "\n",
    "    displace = np.sin(60.0 * x) * np.sin(60.0 * my) * np.sin(60.0 * mz) * 0.1\n",
    "\n",
    "    data = np.sqrt(my**2 + mz**2) * (2.5 + 0.8 * np.sin(x * 50)) + displace\n",
    "    \n",
    "    return (data < 0.25).astype(np.uint8)\n",
    "\n",
    "dim = 256\n",
    "data = generate(dim)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# dense voxels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot = k3d.plot()\n",
    "plot += k3d.voxels(data)\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.grid_visible = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "chunk_size = 16\n",
    "voxelsGroup = []\n",
    "\n",
    "for z,y,x in zip(*np.where(skimage.measure.block_reduce(data, (chunk_size,chunk_size,chunk_size), np.max) > 0)):\n",
    "    chunk = {\n",
    "        \"voxels\": data[z*chunk_size:(z+1)*chunk_size,\n",
    "                       y*chunk_size:(y+1)*chunk_size,\n",
    "                       x*chunk_size:(x+1)*chunk_size],\n",
    "        \"coord\": np.array([x,y,z]) * chunk_size,\n",
    "        \"multiple\": 1\n",
    "    }\n",
    "    \n",
    "    voxelsGroup.append(chunk)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(voxelsGroup), (len(voxelsGroup) * chunk_size ** 3) / (1024 ** 2), data.size / (1024 ** 2)\n",
    "\n",
    "space_size = np.array(data.shape, dtype=np.uint32)[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# voxels_group with list of dicts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "plot = k3d.plot()\n",
    "obj = k3d.voxels_group(space_size, voxelsGroup)\n",
    "plot += obj\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sum(np.dstack([p['voxels'] for p in obj.voxels_group]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Edit object (add/remove some voxels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.mode = 'change'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj.fetch_data('voxels_group')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sum(np.dstack([p['voxels'] for p in obj.voxels_group]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# k3d.voxel_chunk usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "chunks = [k3d.voxel_chunk(g[\"voxels\"], g[\"coord\"], compression_level=1) for g in voxelsGroup]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot = k3d.plot()\n",
    "obj = k3d.voxels_group(space_size, \n",
    "                       chunks_ids=[c[\"id\"] for c in chunks])\n",
    "plot += obj\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for c in chunks:\n",
    "    c.voxels = np.ones_like(c.voxels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(chunks)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot -= obj "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
